﻿using eLoan.Models;
using System;
using System.Web.Mvc;
using System.Linq;

namespace eLoan.Controllers
{
    public class MasterfileController : BaseController
    {

        public ActionResult Index()
        {
            try
            {
                return RedirectToAction("Index", "Home");
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult Borrowers(string branchCode, string searchText = "")
        {
            try
            {

                CheckUserPageAccess(AdministrationHelper.UserPageAccess.AddressBook);

                var db = new DatabaseContext();
                var borrowerListModel = new BorrowerListModel();
                
                borrowerListModel.Branches = AdministrationHelper.GetBranchDropDownListByUserRole(db, User.Identity.Name);
                borrowerListModel.SelectedBranchCode = string.IsNullOrEmpty(branchCode) ? AdministrationHelper.GetLoggedInUserBranchCode(db, User.Identity.Name) : branchCode;
                borrowerListModel.SearchText = searchText;
                borrowerListModel.BorrowerList = MasterFilesHelper.GetBorrwerList(borrowerListModel.SelectedBranchCode, borrowerListModel.SearchText);

                return View(borrowerListModel);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult RefreshBorrowerList(BorrowerListModel model)
        {
            return RedirectToAction("Borrowers", "Masterfile", new { branchCode = model.SelectedBranchCode, searchText = model.SearchText });
        }

        public ActionResult Prospects(string branchCode, int? agentID, string searchText = "")
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Prospects);

                var db = new DatabaseContext();
                var model = new ProspectListModel();

                branchCode = string.IsNullOrEmpty(branchCode) ? AdministrationHelper.GetLoggedInUserBranchCode(db, User.Identity.Name) : branchCode;
                agentID = agentID.HasValue ? agentID : 0;

                var agents = db.Agents.Where(w => w.BranchCode == branchCode);
                var agentList = AdministrationHelper.GetAgentDropDownList(agents).ToList();
                agentList.Insert(0, new SelectListItem() { Value = "0", Text = "All" });

                model.SelectedBranchCode = branchCode;
                model.Branches = AdministrationHelper.GetBranchDropDownList(db.Branches);
                model.SelectedAgentID = agentID.GetValueOrDefault();
                model.Agents = agentList;
                model.SearchText = searchText;
                model.PropsectList = MasterFilesHelper.GetProspectList(branchCode, agentID.GetValueOrDefault(), User.Identity.Name, model.SearchText);

                return View(model);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        [HttpPost]
        public ActionResult RefreshProspectList(ProspectListModel model)
        {
            return RedirectToAction("Prospects", "Masterfile", new { branchCode = model.SelectedBranchCode, agentID = model.SelectedAgentID, searchText = model.SearchText });
        }

        public ActionResult AddEditProspect(string branchCode, int? id)
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Prospects);

                var db = new DatabaseContext();
                var model = MasterFilesHelper.GetProspectAddEditModel(db, branchCode, id);
                return View("ProspectAddEdit", model);
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }

        }

        [HttpPost]
        public ActionResult SaveProspect(ProspectAddEditModel model, string command)
        {
            try
            {
                CheckUserPageAccess(AdministrationHelper.UserPageAccess.Prospects);

                if (ModelState.IsValid)
                {
                    using (var db = new DatabaseContext())
                    {
                        if (command == AdministrationHelper.DBCommand.Add.ToString())
                        {
                            db.Prospects.Add(model.Prospect);
                            db.SaveChanges();

                        }
                        else if (command == AdministrationHelper.DBCommand.Edit.ToString())
                        {
                            db.Entry(model.Prospect).State = System.Data.EntityState.Modified;
                            db.SaveChanges();
                        }
                        else
                        {
                            // cancelled
                        }
                    }
                }

                return RedirectToAction("Prospects", "Masterfile");
            }
            catch (Exception ex)
            {
                return View("Error", ex);
            }
        }

        public ActionResult DeleteProspect(int? id, string branchCode)
        {
            try
            {
                var db = new DatabaseContext();

                var prospect = db.Prospects.Where(w => w.ID == id).SingleOrDefault();

                db.Prospects.Remove(prospect);
                db.SaveChanges();
                return RedirectToAction("Prospects", "Masterfile");
            }
            catch(Exception ex)
            {
                return View("Error", ex);
            }
        }
    }
}
